package com.dingwei.superiordiary.mvp.presenter;

import android.text.TextUtils;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.GoodsListBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by lihao on 2017/9/12.
 */

public class NewSuperiorProductsPresenter extends basePresenter {

    private Contract.NewSuperiorProductsView newSuperiorProductsView;

    public NewSuperiorProductsPresenter(Contract.NewSuperiorProductsView view) {
        super(view);
        this.newSuperiorProductsView = view;
    }

       public void initData(int page, String category_id, String order, String keyword,final boolean isRefresh, boolean isShowDiaog){
           RequestParams rp = new RequestParams(URLS.HOST+URLS.PRODUCT_LIST);
           rp.addBodyParameter("page",page+"");
           if (!TextUtils.isEmpty(category_id)) {
               rp.addBodyParameter("category_id",category_id);
           }
           if (!TextUtils.isEmpty(order)){
               rp.addBodyParameter("order",order);
           }
           if (!TextUtils.isEmpty(keyword)){
               rp.addBodyParameter("keyword",keyword);
           }
           x.http().post(rp, new myCallback(BaseActivity.context, GoodsListBean.class, rp) {
               @Override
               public void back(MyBaseBean baseBean, String result) {
                   GoodsListBean bean = (GoodsListBean) baseBean;
                   if (isRefresh) {
                       newSuperiorProductsView.onRefresh(bean);
                   }else {
                       newSuperiorProductsView.onLoadMore(bean);
                   }
               }

               @Override
               public void fail(FailsBean baseBean, String result) {
                   newSuperiorProductsView.onFail();
               }
           });
       }

}
